package com.zx._01_基础._011_集合;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Test;
import org.springframework.util.ReflectionUtils;

public class HashMapTest {

    //1.简单操作
    @Test
    public void test01() {
        // 1.保存数据
        HashMap<String, Integer> map = new HashMap<>(16);
        map.put("1", 1);
        map.put("2", 2);
        map.put("3", 3);
        map.put("4", 4);

        // 2.使用Entry遍历
        Set<Entry<String, Integer>> entrySet = map.entrySet();
        for (Entry<String, Integer> entry : entrySet) {
            System.out.println(entry);
        }
        // 3. 使用Iterator遍历
        Iterator<Entry<String, Integer>> entryIterator = map.entrySet().iterator();
        while (entryIterator.hasNext()) {
            Map.Entry<String, Integer> next = entryIterator.next();
            System.out.println("key=" + next.getKey() + " value=" + next.getValue());
        }
        // 4. forEach遍历
        map.forEach((key, value) -> {
            System.out.println("key=" + key + " value=" + map.get(key));
        });
    }

    //2.扩容过程
    @Test
    public void test02() throws Exception {
        HashMap<String, Integer> map = new HashMap<>();
        Field field = ReflectionUtils.findField(HashMap.class, "table");
        field.setAccessible(true);
        for (int i = 0; i < 100; i++) {
            map.put("" + i, i);
            Object[] object = (Object[]) field.get(map);// Node 是hashMap的内部类，无法获得到，只能先转换成object[]
            System.out.println("index = " + map.size() + ", capacity = " + object.length);
        }
        /*
            扩容演示日志：
            index = 1, capacity = 16
            index = 2, capacity = 16
            index = 3, capacity = 16
            index = 4, capacity = 16
            index = 5, capacity = 16
            index = 6, capacity = 16
            index = 7, capacity = 16
            index = 8, capacity = 16
            index = 9, capacity = 16
            index = 10, capacity = 16
            index = 11, capacity = 16
            index = 12, capacity = 16
            index = 13, capacity = 32	*****在数组长度要超过 原长度的0.75时，扩容2倍
            index = 14, capacity = 32
            index = 15, capacity = 32
            index = 16, capacity = 32
            index = 17, capacity = 32
            index = 18, capacity = 32
            index = 19, capacity = 32
            index = 20, capacity = 32
            index = 21, capacity = 32
            index = 22, capacity = 32
            index = 23, capacity = 32
            index = 24, capacity = 32
            index = 25, capacity = 64  *****
            index = 26, capacity = 64
            index = 27, capacity = 64
            index = 28, capacity = 64
            index = 29, capacity = 64
            index = 30, capacity = 64
            index = 31, capacity = 64
            index = 32, capacity = 64
            index = 33, capacity = 64
            index = 34, capacity = 64
            index = 35, capacity = 64
            index = 36, capacity = 64
            index = 37, capacity = 64
            index = 38, capacity = 64
            index = 39, capacity = 64
            index = 40, capacity = 64
            index = 41, capacity = 64
            index = 42, capacity = 64
            index = 43, capacity = 64
            index = 44, capacity = 64
            index = 45, capacity = 64
            index = 46, capacity = 64
            index = 47, capacity = 64
            index = 48, capacity = 64
            index = 49, capacity = 128  *****
            index = 50, capacity = 128
            index = 51, capacity = 128
            index = 52, capacity = 128
            index = 53, capacity = 128
            index = 54, capacity = 128
            index = 55, capacity = 128
            index = 56, capacity = 128
            index = 57, capacity = 128
            index = 58, capacity = 128
            index = 59, capacity = 128
            index = 60, capacity = 128
            index = 61, capacity = 128
            index = 62, capacity = 128
            index = 63, capacity = 128
            index = 64, capacity = 128
            index = 65, capacity = 128
            index = 66, capacity = 128
            index = 67, capacity = 128
            index = 68, capacity = 128
            index = 69, capacity = 128
            index = 70, capacity = 128
            index = 71, capacity = 128
            index = 72, capacity = 128
            index = 73, capacity = 128
            index = 74, capacity = 128
            index = 75, capacity = 128
            index = 76, capacity = 128
            index = 77, capacity = 128
            index = 78, capacity = 128
            index = 79, capacity = 128
            index = 80, capacity = 128
            index = 81, capacity = 128
            index = 82, capacity = 128
            index = 83, capacity = 128
            index = 84, capacity = 128
            index = 85, capacity = 128
            index = 86, capacity = 128
            index = 87, capacity = 128
            index = 88, capacity = 128
            index = 89, capacity = 128
            index = 90, capacity = 128
            index = 91, capacity = 128
            index = 92, capacity = 128
            index = 93, capacity = 128
            index = 94, capacity = 128
            index = 95, capacity = 128
            index = 96, capacity = 128
            index = 97, capacity = 256  *****
            index = 98, capacity = 256
            index = 99, capacity = 256
            index = 100, capacity = 256
        */
    }


}
